//
//  CharacterSprite.cpp
//  StarBugs
//
//  Created by jinkwon jeong on 12/11/12.
//  Copyright (c) 2012 antisword@playorca.com. All rights reserved.
//

#include "Sprite/CharacterSprite.h"
#include "Util/ImageManager.h"

int motionData[][2] =
{
    {0, 10}, {10, 10}, {20, 33}, {53, 10}, {55, 12}, {67, 6}, {73, 16},
};

CharacterSprite::~CharacterSprite()
{
    
}

CharacterSprite::CharacterSprite()
{
    char tmp[16];
    
    mainSprite = new CCSprite;
    mainSprite->init();
    mainSprite->autorelease();
    addChild(mainSprite);
    //////
    normalArray = CCArray::create();
    normalArray->retain();
    //    normalMax = 10;
    
    
    ImageManager::openXml(kImageSheet_characterMotion);
    ImageManager::openXml(kImageSheet_characterMotion2);
    
    for(int i = 0; i < 10; i ++)
    {
        sprintf(tmp, "normal_%d", i);
        normalArray->addObject(ImageManager::getFrameWithXml2(kImageSheet_characterMotion, tmp));
    }
    
    for(int i = 0; i < 10; i ++)
    {
        sprintf(tmp, "blink_%d", i);
        normalArray->addObject(ImageManager::getFrameWithXml2(kImageSheet_characterMotion, tmp));
    }
    
    for(int i = 0; i < 33; i ++)
    {
        sprintf(tmp, "sleep_%d", i + 1);
        normalArray->addObject(ImageManager::getFrameWithXml2(kImageSheet_characterMotion, tmp));
    }
    
    for(int i = 0; i < 2; i ++)
    {
        sprintf(tmp, "laugh_%d", i + 1);
        normalArray->addObject(ImageManager::getFrameWithXml2(kImageSheet_characterMotion, tmp));
    }
    
    for(int i = 0; i < 12; i ++)
    {
        sprintf(tmp, "hello_%d", i + 1);
        normalArray->addObject(ImageManager::getFrameWithXml2(kImageSheet_characterMotion, tmp));
    }
    for(int i = 0; i < 6; i ++)
    {
        sprintf(tmp, "up_%d", i + 1);
        normalArray->addObject(ImageManager::getFrameWithXml2(kImageSheet_characterMotion2, tmp));
    }
    for(int i = 0; i < 16; i ++)
    {
        sprintf(tmp, "fail_%d", i + 1);
        normalArray->addObject(ImageManager::getFrameWithXml2(kImageSheet_characterMotion2, tmp));
    }
    
    ImageManager::closeXml(kImageSheet_characterMotion);
    ImageManager::closeXml(kImageSheet_characterMotion2);
    mainSprite->setDisplayFrame((CCSpriteFrame*)normalArray->objectAtIndex(0));
    num = 0;
    motionType = motionType_normal;
    schedule(schedule_selector(CharacterSprite::motionTick), 0.1);
}

void CharacterSprite::motionTick(float dt)
{
    num = num + 1;
    int motionNum = num;
    
    if(motionType == motionType_laugh)
        motionNum = num % 2;
    
    if(motionNum + motionData[motionType][0] >= normalArray->count())
    {
        num = 0;
        motionType = motionType_normal;
    }
    else
        mainSprite->setDisplayFrame((CCSpriteFrame *)normalArray->objectAtIndex(motionNum + motionData[motionType][0]));
    
    if(num == motionData[motionType][1] -1)
    {
        int rnd = (int)(CCRANDOM_0_1() * 10);
        if(rnd == 0)
            motionType = motionType_blink;
        else if(rnd == 1)
            motionType = motionType_sleep;
        else if(rnd == 2)
            motionType = motionType_laugh;
        else if(rnd == 3)
            motionType = motionType_hello;
        else
            motionType = motionType_normal;
        
        num = 0;
    }
}

void CharacterSprite::startAim()
{
    unschedule(schedule_selector(CharacterSprite::motionTick));
    num = 0;
    motionType = motionType_aim;
    schedule(schedule_selector(CharacterSprite::aimTick), 0.1);
}

void CharacterSprite::aimTick(float dt)
{

    num = num + 1;
    int motionNum = num;
    motionNum = num % motionData[motionType][1];
    mainSprite->setDisplayFrame((CCSpriteFrame *)normalArray->objectAtIndex(motionNum + motionData[motionType][0]));
}

void CharacterSprite::endAim()
{
    unschedule(schedule_selector(CharacterSprite::aimTick));
    num = 0;
    motionType = motionType_normal;
    schedule(schedule_selector(CharacterSprite::motionTick), 0.1);
}

void CharacterSprite::startFail()
{
    unschedule(schedule_selector(CharacterSprite::motionTick));
    num = 0;
    motionType = motionType_fail;
    schedule(schedule_selector(CharacterSprite::failTick), 0.1);
}

void CharacterSprite::failTick(float dt)
{
    num = num + 1;
    int motionNum = num;
    motionNum = num % motionData[motionType][1];
    mainSprite->setDisplayFrame((CCSpriteFrame *)normalArray->objectAtIndex(motionNum + motionData[motionType][0]));
}

void CharacterSprite::endFail()
{
    unschedule(schedule_selector(CharacterSprite::failTick));
    num = 0;
    motionType = motionType_normal;
    schedule(schedule_selector(CharacterSprite::motionTick), 0.1);
}

void CharacterSprite::pause()
{
    pauseSchedulerAndActions();
}

void CharacterSprite::resume()
{
    resumeSchedulerAndActions();
}